package com.jcfk.eam.controller.mould;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.*;
import com.jcfk.eam.domain.dto.mould.EamMouldPackageRecordDTO;
import com.jcfk.eam.domain.query.mould.EamMouldPackageRecordQuery;
import com.jcfk.eam.service.mould.EamMouldPackageRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 模板框记录
 *
 * @author ly
 * @email 522891237@gqq.com
 * @date 2025-08-21
 */
@Api(tags = {"模板框记录管理"})
@RestController
@RequestMapping("eammouldpackagerecord")
public class EamMouldPackageRecordController {
    @Autowired
    private EamMouldPackageRecordService eamMouldPackageRecordService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamMouldPackageRecordQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询模板框记录")
    @PostMapping("/queryByPage")
    public RetPage<EamMouldPackageRecordDTO> queryByPage(@RequestBody ReqPageParam<EamMouldPackageRecordQuery> requestParam) {
        if (requestParam.getSort().getOrderItem() == null) {
            ReqSortParam<EamMouldPackageRecordQuery> sortParam = new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        DataPage<EamMouldPackageRecordDTO> page = new DataPage<EamMouldPackageRecordDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamMouldPackageRecordDTO.class);
        page = eamMouldPackageRecordService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 模板框记录
     * 根据id 查询详情
     */
    @ApiOperation("查询模板框记录")
    @GetMapping("/get/{id}")
    public RetResult<EamMouldPackageRecordDTO> get(@PathVariable("id") String id) {
        EamMouldPackageRecordDTO item = eamMouldPackageRecordService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理模板框记录
     */
    @ApiOperation("保存模板框记录")
    @PostMapping("/save")
    @RequestLogHeader(modular = "模板框记录管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamMouldPackageRecordDTO eamMouldPackageRecord) {
        // 数据校验
        RetResult ret = eamMouldPackageRecordService.isValid(eamMouldPackageRecord);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamMouldPackageRecord.getId())) {
            // 新增
            eamMouldPackageRecordService.insert(eamMouldPackageRecord);
        } else {
            // 更新
            eamMouldPackageRecordService.update(eamMouldPackageRecord);
        }
        return RetResult.ok();

    }

    /**
     * 批量删除模板框记录
     */
    @ApiOperation("批量删除模板框记录")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "模板框记录管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamMouldPackageRecordDTO eamMouldPackageRecord) {
        return RetResult.ok(eamMouldPackageRecordService.logicDeleteList(eamMouldPackageRecord));
    }


    /**
     * 删除模板框记录
     */
    @ApiOperation("删除模板框记录")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "模板框记录管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamMouldPackageRecordService.deleteItem(id);
        return RetResult.ok();
    }
}
